package com.pig4cloud.pig.hu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.hu.entity.Activity;
import com.pig4cloud.pig.hu.service.ActivityService;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/carActivity")
@RequiredArgsConstructor
@CrossOrigin
public class ActivityController {

	private final ActivityService activityService;

	/*
	添加
	 */
	@PostMapping
	public R addCustomer(@RequestBody Activity activity){
		activityService.checkUnique(activity);
		activity.setCreateBy(SecurityUtils.getUser().getName());
		return R.ok(activityService.save(activity),"添加成功");
	}

	/*
	分页查询
	 */
	@GetMapping("/page")
	public R<IPage<Activity>> getDictPage(@ParameterObject Page page, @ParameterObject Activity activity) {
		LambdaQueryWrapper<Activity> queryWrapper = Wrappers.<Activity>lambdaQuery()
				.like(StrUtil.isNotBlank(activity.getActivityName()),
						Activity::getActivityName,
						activity.getActivityName())
				.orderByDesc(Activity::getActivityId);
		IPage<Activity> activityPage = activityService.page(page,queryWrapper);
		return R.ok(activityPage);

	}
	/*
	修改
	 */
	@PutMapping
	public R updateActivity(@RequestBody Activity activity){
		activityService.checkUnique(activity);
		return R.ok(activityService.updateById(activity));
	}
	/*
	活动上下架
	 */
	@PutMapping("/job/{ActivityId}")
	public boolean toggleStatus(@PathVariable Long ActivityId) {
		return activityService.toggleStatus(ActivityId);
	}

	/*
	根据id查
	 */
	@GetMapping("/{activityId}")
	public R<Activity> selectById(@PathVariable Long activityId){
		return R.ok(activityService.getById(activityId));
	}
	@DeleteMapping
	public R delById(@RequestBody Long[] activityId){
		return R.ok(activityService.removeByIds(Arrays.asList(activityId)));
	}
}
